@model System.String
@using Telerik.Web.Mvc.UI
@{
    List<string> allImages = new List<string>();
    if (@Model != string.Empty && @Model != null)
    {
        string GalleryPath = Server.MapPath("~/Content/UserFiles/Users/" + @Model + "/");
        if (!Directory.Exists(GalleryPath))
        {
            Directory.CreateDirectory(GalleryPath);
        }
        
        string RootPath = Server.MapPath("~");
        string[] AllFiles = Directory.GetFiles(Server.UrlDecode(GalleryPath));

        for (int i = 0; i < AllFiles.Length; i++)
        {
            AllFiles[i] = AllFiles[i].Replace(RootPath, "");
            AllFiles[i] = AllFiles[i].Replace("\\", "/").ToLower();
            if (AllFiles[i].EndsWith(".jpg") || AllFiles[i].EndsWith(".png") || AllFiles[i].EndsWith(".gif") || AllFiles[i].EndsWith(".bmp"))
            {
                allImages.Add("/Image?img=/" + HttpUtility.UrlEncode(AllFiles[i]) + "&w=120&h=120&t=0&c=0");
            }
        }
    }
}

<br class="clear" />
<div class="gallery_editor_wrap gallery_editor_wrap_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))">
    @{
        int i1 = 0;
        foreach (string item in allImages)
        {
            i1 = i1 + 1;
            <div class="gallery_div gallery_div_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)) gallery_img_@(i1)_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))">
                <img src="@item" alt="" />
                <input type="checkbox" class="gallery_img_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))" id="gallery_img_@(i1)_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))" />
                <span for="gallery_img_@(i1)_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))">מחק</span>
            </div>
        }
        i1++;
    }
</div>
<br />
@(Html.Telerik().Upload()
        .Name("attachments_" + ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))
        .Multiple(true)
        .Async(async => async
            .Save("Save", "GalleryUser", new { name = @Model, area = "" })
            .Remove("Remove", "GalleryUser", new { name = @Model, area = "" })
            .AutoUpload(true)
        )
        .ClientEvents(events => events.OnSuccess("OnSuccess_" + ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)).OnLoad("OnLoad_" + ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)))
        .HtmlAttributes(new { @class = "gallery-upload-box" })
)

<script type="text/javascript">

    var i11 = @(i1);

    function OnSuccess_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))(e) {
        if (e.operation == "upload") {
            var imgUrl = "/Image?img=/" + encodeURIComponent("Content/UserFiles/Users/@Model/" + getFileInfo(e));
            var divCont = '<div class="gallery_div '+getFileInfo(e)+' gallery_div_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)) gallery_img_'+i11+'_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))"><img src="'+imgUrl+'&w=120&h=120&t=0&c=0" alt="" /><input type="checkbox" class="gallery_img_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))" id="gallery_img_'+i11+'_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))" /><span for="gallery_img_'+i11+'_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))">מחק</span></div>';
            //var divCont = '<div class="'+getFileInfo(e)+' gallery_div_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)) gallery_img_'+i11+'_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))"><img src="'+imgUrl+'&w=120&h=120&t=0&c=0" alt="" /></div>';
            $(".gallery_editor_wrap_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))").append(divCont);
            i11 = i11 + 1;
        }
        else if (e.operation == "remove") {
            $('div[class*="'+getFileInfo(e)+'"]').remove();
        }
    }

    var PARAM_NAME = "attachments";
    function OnLoad_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))(e) {
        var uploadElement = $("input", this).attr("name", PARAM_NAME);
        setTimeout(function () {
            uploadElement.data("tUpload").name = PARAM_NAME;
        }, 0);
    }

    function getFileInfo(e) {
        return $.map(e.files, function (file) {
            var info = file.name;
            return info;
        }).join(", ");
    }
</script>
@{ Html.Telerik().ScriptRegistrar().OnDocumentReady(@<text> 
    $(".gallery_img_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))").live("click", function(){ 
        var itemid = $(this).attr("id");
        var imgPaht = $("." + itemid + " img").attr("src");
        $.post("/GalleryUser/RemoveFile", { name: "@Model", fullName: imgPaht } )
            .success(function() { 
                $("." + itemid).remove();
             });
        return false;
    });
</text>); }